import conf, time
from engine import Widgets, reactor
from abcommon import *
from lib.libftp import *
import gobject

class UI(Widgets):
	_instance = None
	
	def __init__(self):
		#super(UI, self).__init__()
		Widgets.__init__(self)
		
		self.__widget = self.XMLtree.getWidget(conf.WINDOW_TYPE)
		self.__set(
				"%s %s" % (conf.APP_NAME, conf.APP_VERSION),
				conf.DATA_DIR + "/img/logo_minimal.png")
		
		self.ftp = Invoker(FTPConnection)
		self.ftp.connect('connected', self.list)
		self.connectEvents()
		
	def __set(self, title, icon):
		self.__widget.set_title(title)
		self.__widget.set_icon_from_file(icon)
		
	def connectEvents(self):
		self.XMLtree.connectSignals({
			"on_about" 					 : self._loadAboutDialog, 
			"on_AbsynthMain_destroy" 	 : self._quitMessage,
			"on_ConnectionButton_clicked": self.__getFTPConnection,
			"on_Esci_menuitem_clicked"	 : self._quitMessage,
		})
 		
 	def _quitMessage(self, *args):
 		print "Goodbye, my darling Clementine!"
 		self.ftp.quit()
 		reactor.stop()
 		
 	def _loadAboutDialog(self, *args):
 		Widgets.selectWidget("AboutDialog").UI()
 		
 	def __getFTPConnection(self, *args):
 		self.__getWidgets = {
			"hostname":	self.XMLtree.getWidget("HostnameBox"),
			"username": self.XMLtree.getWidget("UsernameBox"),
			"password": self.XMLtree.getWidget("PasswordBox")
		}
 		
 		self.__connectionData = {
			"hostname":	self.__getWidgets["hostname"].get_text(),
			"username":	self.__getWidgets["username"].get_text(),
			"password":	self.__getWidgets["password"].get_text()
		}
 		
 		self.ftp.connectTo(
			self.__connectionData["hostname"],
			self.__connectionData["username"],
			self.__connectionData["password"])
 		
 		LogView = Invoker(self.selectWidget("LogView").UI)
		LogView.write("Connesso a ftp://%s@%s con password %s" % (
			self.__connectionData["hostname"],
			self.__connectionData["username"],
			self.__connectionData["password"]))
		LogView.write(self.ftp.getWelcome())
		
	def list(self, *args):
		RemoteView = Invoker(Widgets.selectWidget("RemoteView").UI)
		RemoteView.loadRows()
		
		